package com.lp.controller.iot;

import com.lp.bo.IotVisualDisplayInfoBO;
import com.lp.bo.UserInfoBO;
import com.lp.common.Code;
import com.lp.common.Constants;
import com.lp.common.ErrorCodeEnum;
import com.lp.controller.BaseController;
import com.lp.util.CommonUtil;
import com.lp.util.ObjectUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @author thomas.he
 * @Description:
 * @date 2021/7/27 21:32
 */
@Controller
public class IotVisualDisplayInfoController extends BaseController {
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST}, value = {"/page/visual/display"})
    public ModelAndView selectPage(HttpServletResponse response, @RequestHeader(value = "USER-KEY", required = true) String userKey, @RequestBody IotVisualDisplayInfoBO obj, @RequestParam(required = false) Integer pageSize, @RequestParam Integer paged) {
        Map resultMap = getResultMap();
        try {
            resultMap = this.service.selectPageList("IotVisualDisplayInfo.selectPage", getPageBean(paged, pageSize), obj, new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{obj});
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST}, value = {"/visual/display/sub/all"})
    public ModelAndView selectList(HttpServletResponse response, @RequestHeader(value = "USER-KEY", required = true) String userKey, @RequestBody IotVisualDisplayInfoBO obj) {
        Map resultMap = getResultMap();
        try {
            List list = new ArrayList();
            Map resultData = this.service.selectOne("IotVisualDisplayInfo.selectOne", obj, new Object[0]);
            if (isOk(resultData)) {
                list.add((IotVisualDisplayInfoBO) getData(resultData));
                IotVisualDisplayInfoBO searchData = new IotVisualDisplayInfoBO();
                searchData.setParent_id(obj.getId());
                resultMap = this.service.selectList("IotVisualDisplayInfo.select", searchData, new Object[0]);
                if (isOk(resultMap)) {
                    list.addAll((Collection) getData(resultMap));
                }
                putData(resultMap, list);
                putStatusCode(resultMap, Code.ResponseCode.SystemCode.OK.intValue());
            }
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{obj});
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST}, value = {"/visual/display"})
    public ModelAndView save(HttpServletResponse response, @RequestHeader(value = "USER-KEY", required = true) String userKey, @RequestBody IotVisualDisplayInfoBO obj) {
        Map resultMap = getResultMap();
        try {
            IotVisualDisplayInfoBO tmp;
            IotVisualDisplayInfoBO tmp2;
            List<com.lp.bo.IotVisualDisplayInfoBO> subList = null;

            if (ObjectUtil.isNotEmpty(obj.getKeycode())) {
                tmp = new IotVisualDisplayInfoBO();
                tmp.setKeycode(obj.getKeycode());
                resultMap = this.service.selectOne("IotVisualDisplayInfo.selectOne", tmp, new Object[0]);
                if (isOk(resultMap)) {
                    tmp = (IotVisualDisplayInfoBO) getData(resultMap);
                    obj.setContent(tmp.getContent());
                    obj.setConfig(tmp.getConfig());
                }

                tmp2 = new IotVisualDisplayInfoBO();
                tmp2.setParent_id(tmp.getId());
                subList = (List) getData(this.service.selectList("IotVisualDisplayInfo.select", tmp2, new Object[0]));
            }
            obj.setKeycode(CommonUtil.UUIDString.getUUIDString());
            resultMap = this.service.insert("IotVisualDisplayInfo.insert", obj, new Object[0]);

            if ((ObjectUtil.isNotEmpty(obj.getKeycode())) && (ObjectUtil.isNotEmpty(subList).booleanValue())) {
                for (Iterator<IotVisualDisplayInfoBO> iterator = subList.iterator(); iterator.hasNext(); ) {
                    tmp = iterator.next();
                    tmp.setId(null);
                    tmp.setKeycode(CommonUtil.UUIDString.getUUIDString());
                    tmp.setParent_id(obj.getId());
                    tmp.setScene_id(obj.getScene_id());
                    this.service.insert("IotVisualDisplayInfo.insert", tmp, new Object[0]);
                }
            }else{
                obj.setScene_id(-1);
            }
            putData(resultMap, obj);
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{obj});
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.GET}, value = {"/visual/display"})
    public ModelAndView selectOne(HttpServletResponse response, @RequestParam(required = false) Integer id, @RequestParam(required = false) String keycode) {
        Map resultMap = getResultMap();
        try {
            IotVisualDisplayInfoBO tmp = new IotVisualDisplayInfoBO();
            tmp.setId(id);
            tmp.setKeycode(keycode);
            resultMap = this.service.selectOne("IotVisualDisplayInfo.selectOne", tmp, new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{id});
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.GET}, value = {"/{vistorname}"})
    public String firstIndex(HttpServletRequest req, HttpServletResponse resp, @PathVariable String vistorname) {
        Map resultData = getResultMap();
        try {
            IotVisualDisplayInfoBO tmp = new IotVisualDisplayInfoBO();
            tmp.setVisitorname(vistorname);
            tmp = (IotVisualDisplayInfoBO) getData(this.service.selectOne("IotVisualDisplayInfo.selectOne", tmp, new Object[0]));
            if (ObjectUtil.isNotEmpty(tmp).booleanValue()) {
                if ((tmp.getOpenflag().intValue() == 1) && (ObjectUtil.isNotEmpty(tmp.getPassword()))) {
                    tmp.setContent("");
                    req.setAttribute("info", tmp);
                    return "/oss/iot/visual_login";
                }

                return "redirect:/page/visual/show.html?keyCode=" + tmp.getKeycode();
            }

            try {
                PrintWriter out = resp.getWriter();
                out.write("no app info!");
                out.flush();
            } catch (IOException localIOException) {
            }
        } catch (Exception e) {
            exception(e, resultData, new Object[0]);
        }
        return null;
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST}, value = {"/visual/login"})
    public ModelAndView visualLogin(HttpServletResponse response, @RequestBody IotVisualDisplayInfoBO obj) {
        Map resultMap = getResultMap();
        try {
            IotVisualDisplayInfoBO tmp = new IotVisualDisplayInfoBO();
            tmp.setKeycode(obj.getKeycode());
            tmp = (IotVisualDisplayInfoBO) getData(this.service.selectOne("IotVisualDisplayInfo.selectOne", tmp, new Object[0]));
            if (ObjectUtil.isNotEmpty(tmp).booleanValue()) {
                if (obj.getPassword().equalsIgnoreCase(tmp.getPassword())) {
                    UserInfoBO user = (UserInfoBO) getData(this.service.selectOne("UserInfo.selectOne", new UserInfoBO(tmp.getMid()), new Object[0]));
                    user.setPassword("");
                    putData(resultMap, user);
                   }
                putStatusCode(resultMap, Code.ResponseCode.SystemCode.PASSWORD_ERROR.intValue());
               }
            putStatusCode(resultMap, Code.ResponseCode.SystemCode.PASSWORD_ERROR.intValue());
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{obj});
        }
        label171:
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.PUT}, value = {"/visual/display"})
    public ModelAndView update(HttpServletResponse response, @RequestHeader(value = "USER-KEY", required = true) String userKey, @RequestBody IotVisualDisplayInfoBO obj) {
        Map resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            if (ObjectUtil.isNotEmpty(obj.getId()).booleanValue()) {
                if (ObjectUtil.isNotEmpty(obj.getVisitorname())) {
                    IotVisualDisplayInfoBO dt = new IotVisualDisplayInfoBO();
                    dt.setVisitorname(obj.getVisitorname());
                    List list = (List) getData(this.service.selectList("IotVisualDisplayInfo.select", dt, new Object[0]));
                    if ((ObjectUtil.isNotEmpty(list).booleanValue()) &&
                            (((IotVisualDisplayInfoBO) list.get(0)).getId().intValue() + 0 != obj.getId().intValue())) {
                        putStatusCode(resultMap, ErrorCodeEnum.VISITOR_NAME_REPEAT.getCode().intValue());
                        putStatusMsg(resultMap, ErrorCodeEnum.VISITOR_NAME_REPEAT.getDesc());
                        return getModelAndView(response, resultMap);
                    }
                }
                obj.setMid(user.getId());
                resultMap = this.service.update("IotVisualDisplayInfo.update", obj, new Object[0]);
            }
            obj.setMid(user.getId());
            resultMap = this.service.update("IotVisualDisplayInfo.updateKeyCode", obj, new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{obj});
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.DELETE}, value = {"/visual/display"})
    public ModelAndView delete(HttpServletResponse response, @RequestParam Integer id) {
        Map resultMap = getResultMap();
        try {
            IotVisualDisplayInfoBO obj = new IotVisualDisplayInfoBO();
            if (ObjectUtil.isEmpty(id).booleanValue()) {
                putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR.intValue());
            }
            obj.setId(id);
            obj.setDelete_flag(Constants.DELETE.YES);
            resultMap = this.service.update("IotVisualDisplayInfo.update", obj, new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{id});
        }
        label90:
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST}, value = {"/scene/visual"})
    public ModelAndView selectScene(HttpServletResponse response, @RequestHeader(value = "USER-KEY", required = true) String userKey, @RequestBody IotVisualDisplayInfoBO obj) {
        Map resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            obj.setUser_id(user.getId());
            resultMap = this.service.selectList("IotVisualDisplayInfo.selectSceneVisual", obj, new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[]{obj});
        }
        return getModelAndView(response, resultMap);
    }
}
